package com.lzc.blog.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzc.blog.admin.mapper.PermissionMapper;
import com.lzc.blog.admin.model.params.PageParam;
import com.lzc.blog.admin.pojo.Permission;
import com.lzc.blog.admin.service.PermissionService;
import com.lzc.blog.admin.vo.PageResult;
import com.lzc.blog.admin.vo.Result;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public Result listPermission(PageParam pageParam) {
        /**
         * 要的数据：
         * 表的所有字段： Permission
         * 分页查询
         */
        IPage<Permission> page=new Page(pageParam.getCurrentPage(),pageParam.getPageSize());
        LambdaQueryWrapper<Permission> queryWrapper=new LambdaQueryWrapper<>();
         if (!StringUtils.isEmptyOrWhitespaceOnly(pageParam.getQueryString())){
             // and name=?
             queryWrapper.eq(Permission::getName,pageParam.getQueryString());
         }
        IPage<Permission> permissionIPage = permissionMapper.selectPage(page, queryWrapper);
        PageResult<Permission> pageResult=new PageResult<>();
        pageResult.setList(permissionIPage.getRecords());
        pageResult.setTotal(permissionIPage.getTotal());
        System.out.println(permissionIPage.getTotal());
        return Result.success(pageResult);

    }

    @Override
    public Result add(Permission permission) {
        permissionMapper.insert(permission);
        return Result.success(null);
    }

    @Override
    public Result update(Permission permission) {
        permissionMapper.updateById(permission);
        return Result.success(null);
    }

    @Override
    public Result delete(Long id) {
        permissionMapper.deleteById(id);
        return Result.success(null);
    }

    @Override
    public List<Permission> findPermissionByAdminId(Long id) {
        List<Permission> permissionList= this.permissionMapper.findPermissionByAdminId(id);
        return permissionList;
    }
}
